package com.minglv.mini.service.impl;

import com.minglv.mini.domain.Activity;
import com.minglv.mini.domain.ActivityApply;
import com.minglv.mini.domain.IntegralLog;
import com.minglv.mini.domain.User;
import com.minglv.mini.domain.dto.ActivityApplyDto;
import com.minglv.mini.mapper.ActivityApplyMapper;
import com.minglv.mini.mapper.ActivityMapper;
import com.minglv.mini.mapper.IntegralLogMapper;
import com.minglv.mini.mapper.UserMapper;
import com.minglv.mini.service.IActivityApplyService;
import com.minglv.mini.tools.DateUtil;
import com.minglv.mini.tools.JSONUtils;
import com.minglv.mini.tools.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

@Service
//@DataSource(DataSourceType.SLAVE)
public class ActivityApplyServiceImpl implements IActivityApplyService {
    @Autowired
    private ActivityApplyMapper activityApplyMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private ActivityMapper activityMapper;

    @Resource
    private IntegralLogMapper integralLogMapper;

    @Override
//    @DataSource(DataSourceType.SLAVE)
    public Object postActiveSign(String token, String code) {
        String[] s = token.split(" ");
        token = s[1];
        Long userId = JWTUtils.getUserid(token);
        User user = userMapper.selectUserById(userId);
        if (user.getIsAdmin() == 1) {
            ActivityApply aa = new ActivityApply();
            aa.setCode(code);
            List<ActivityApply> activityApplies = activityApplyMapper.selectActivityApplyList(aa);
            if (!CollectionUtils.isEmpty(activityApplies)) {
                ActivityApply activityApply = activityApplies.get(0);
                if (activityApply.getStatus() == 2) {
                    Map reMap  = new HashMap();
                    reMap.put("status",200);
                    reMap.put("msg","已在:" + DateUtil.format(activityApply.getSignTime())  + "签到!!");
                    return reMap;
                }
                activityApply.setStatus(2);
                activityApply.setSignTime(DateUtil.now());
                //  返佣积分
                if (!ObjectUtils.isEmpty(activityApply.getShareUid()) && activityApply.getShareUid() != 0) {
                    Activity activity = activityMapper.selectActivityById(activityApply.getActiveId());
                    User user1 = userMapper.selectUserById(activityApply.getShareUid());
                    if (!ObjectUtils.isEmpty(user1)) {
                        user1.setIntegral(user1.getIntegral().add(new BigDecimal(activity.getSignJf())));
                        userMapper.updateUser(user1);
                        IntegralLog ll = new IntegralLog();
                        Long maxId = integralLogMapper.getMaxId();
                        ll.setId(maxId + 1);
                        ll.setUserId(activityApply.getShareUid());
                        ll.setIntegral(new BigDecimal(activity.getSignJf()));
                        ll.setRemark("推荐报名奖励");
                        ll.setFromUser(activityApply.getUserId());
                        ll.setActiveId(activity.getId());
                        integralLogMapper.insertIntegralLog(ll);
                    }
                }
                Map reMap  = new HashMap();
                reMap.put("status",200);
                reMap.put("msg","签到成功！");
                return reMap;
            }else {
                Map reMap  = new HashMap();
                reMap.put("status",201);
                reMap.put("msg","请核对签到码是否正确");
                return reMap;
            }
        }else{
            Map reMap  = new HashMap();
            reMap.put("status",201);
            reMap.put("msg","账户不是管理员");
            return reMap;
        }
    }

    @Override
    public List<Map> getInviteList(String token) {

        Long userId = JWTUtils.getUserid(token);
        ActivityApply aa = new ActivityApply();
        aa.setShareUid(userId);
        List<ActivityApply> activityApplies = activityApplyMapper.selectActivityApplyList(aa);
        if (!CollectionUtils.isEmpty(activityApplies)) {
            List< Map>  res = new ArrayList<>();
            for (ActivityApply activityApply : activityApplies) {
                Map userMap = userMapper.getUserMap(activityApply.getShareUid());
                res.add(userMap);
            }
            return res;
        }
        return Collections.emptyList();
    }

    /**
     * 报名列表
     *
     * @param id
     * @param searchName
     * @param status
     * @return
     */
    @Override
    public List<ActivityApplyDto> activeApplyList(Long id, String searchName, Integer status) {
        return activityApplyMapper.activityApplyList(id, searchName, status);
    }

    @Override
//    @DataSource(DataSourceType.SLAVE)
    public Map getActiveInfo(Long id) {
        Map reMap =  activityApplyMapper.getActiveInfo(id);
        reMap.put("forms", JSONUtils.json2Map(reMap.get("forms").toString()));
        return reMap;
    }

    @Override
//    @DataSource(DataSourceType.SLAVE)
    public String getQrcode(Long id) {
        ActivityApply activityApply = activityApplyMapper.selectActivityApplyById(id);
        Random random = new Random();
        String code =  DateUtil.getTimestamp() + "_" + (random.nextInt(99) + 1);
        activityApply.setCode(code);
        activityApplyMapper.updateActivityApply(activityApply);
        return code;
    }

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public ActivityApply selectActivityApplyById(Long id)
    {
        return activityApplyMapper.selectActivityApplyById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param activityApply 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<ActivityApply> selectActivityApplyList(ActivityApply activityApply)
    {
        return activityApplyMapper.selectActivityApplyList(activityApply);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param activityApply 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertActivityApply(ActivityApply activityApply)
    {
        return activityApplyMapper.insertActivityApply(activityApply);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param activityApply 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateActivityApply(ActivityApply activityApply)
    {
        return activityApplyMapper.updateActivityApply(activityApply);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteActivityApplyByIds(Long[] ids)
    {
        return activityApplyMapper.deleteActivityApplyByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteActivityApplyById(Long id)
    {
        return activityApplyMapper.deleteActivityApplyById(id);
    }


}
